import React from 'react';
import fav from '../../assets/css/user/fav.module.css'
import {Link} from "react-router-dom";
import {Modal,Toast} from 'antd-mobile';
import 'antd-mobile/lib/modal/style/css'

export default class FavComponent extends React.Component{
    constructor(){
        super();
        this.state={
            uid:localStorage.uid,
            good:[]
        }
    }
    goBack(){
        this.props.history.goBack()
    }
    componentWillMount() {
        this.getData()
    }
    getData(){
        fetch('http://vueshop.glbuys.com/api/user/fav/index?uid='+this.state.uid+'&token=1ec949a15fb709370f&page=1')
            .then((res)=>{
                return res.json()
            }).then(res=>{
            // console.log(res.data);
            if (res.code === 200) {
                this.setState({good:res.data})
            }
        });
    }
    Del(index){
        const alert = Modal.alert;
        alert('', '确认要删除吗', [
            { text: '取消', onPress: () => console.log('cancel') },
            { text: '确认', onPress: () => {
                    fetch("http://vueshop.glbuys.com/api/user/fav/del?uid="+this.state.uid+"&fid="+index+"&token=1ec949a15fb709370f")
                        .then(res=>{return res.json()})
                        .then(res=>{
                            Toast.info(res.data, 1);
                            this.getData();
                            this.props.history.go();
                        })
                }},
        ])
    }

    render() {
        return(
            <div className={fav.boxFav}>
                <div>
                    <img src={require("../../assets/images/home/goods/back.png")} onClick={this.goBack.bind(this)} alt=""/>
                    <span>我的收藏</span>
                </div>
                <ul className={fav.boxUl}>
                    {
                        // this.state.good!=null?
                            this.state.good.map((item,index)=>{
                                return(
                                    <li key={index}>
                                        <img src={item.image} alt=''/>
                                        <p>{item.title}</p>
                                        <p>￥{item.price}</p>
                                        <Link style={{color:'red'}} to={"/goods/details/item?gid="+item.gid}>
                                            <button>购买</button>
                                        </Link>
                                        <button onClick={this.Del.bind(this,item.fid)}>删除</button>
                                    </li>
                                )
                            })
                    }
                </ul>
            </div>
        )
    }
}